package com.chookss.home.gallery.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.home.entity.GalleryListEntity;
import com.chookss.home.gallery.GalleryListActivity;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.Utils;
import com.chookss.view.DrawableTextView;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class GalleryListAdapter extends BaseQuickAdapter<GalleryListEntity, BaseViewHolder> {
    private Activity context;
    private String type;//传值所需 1-日期 2-标签 3 地点
    private int currentType = 1;//当前页面选择的 1-日期 2-标签 3 地点
    private int roleType = 1; //roleType 0个人 1全部
    private String catalogCodes = "", city = "", createTime = "", tagCode = ""; //目录code
    private String groupCode = "";
    private GridSpacingItemDecoration gridSpacingItemDecoration;

    public GalleryListAdapter(int layoutResId, Activity context, @Nullable List<GalleryListEntity> data, String type, int roleType) {
        super(layoutResId, data);
        this.context = context;
        this.type = type;
        this.roleType = roleType;
        gridSpacingItemDecoration = new GridSpacingItemDecoration(4, Utils.dip2px(context, 2), false);
    }

    @Override
    protected void convert(BaseViewHolder helper, GalleryListEntity item) {
        TextView tvTitle = helper.getView(R.id.tvTitle);
        RecyclerView rv = helper.getView(R.id.rvView);
        DrawableTextView tvMore = helper.getView(R.id.tvMore);
        if (currentType == 3) {
            if (Utils.isNull(item.getTypeName())) {
                tvTitle.setText("未知");
            } else {
                if (item.getTypeName().equals("a")) {
                    tvTitle.setText("未知");
                } else {
                    tvTitle.setText(item.getTypeName());
                }
            }
        } else {
            tvTitle.setText(item.getTypeName());
        }
        tvMore.setText(item.getImageCount());
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(context, 4);
        rv.setLayoutManager(gridLayoutManager1);
        if (rv.getItemDecorationCount() == 0) {
            rv.addItemDecoration(gridSpacingItemDecoration);
        }
        GalleryAdapter adapter = null;
        int num = Utils.changeNumber(item.getImageCount());
        if (num <= 4) {
            tvMore.setVisibility(View.GONE);
            adapter = new GalleryAdapter(R.layout.item_gallery, context, item.getImagePathList(), 0);
        } else if (num < 8) {
            tvMore.setVisibility(View.VISIBLE);
            adapter = new GalleryAdapter(R.layout.item_gallery, context, item.getImagePathList().subList(0, 4), 0);
        } else {
            tvMore.setVisibility(View.VISIBLE);
            adapter = new GalleryAdapter(R.layout.item_gallery, context, item.getImagePathList().subList(0, 8), 0);
        }
        rv.setAdapter(adapter);
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GalleryListActivity.class);
                intent.putExtra("data", tvTitle.getText().toString());
                if (currentType == 1) {
                    intent.putExtra("createTime", item.getTypeName());
                    intent.putExtra("tagCode", tagCode);
                    intent.putExtra("city", city);
                } else if (currentType == 2) {
                    intent.putExtra("createTime", createTime);
                    intent.putExtra("tagCode", item.getTypeCode());
                    intent.putExtra("city", city);
                } else if (currentType == 3) {
                    intent.putExtra("createTime", createTime);
                    intent.putExtra("tagCode", tagCode);
                    intent.putExtra("city", item.getTypeName());
                }
                intent.putExtra("catalogCodes", catalogCodes);
                intent.putExtra("searchType", type);
                intent.putExtra("roleType", roleType);
                context.startActivity(intent);
            }
        });
    }

    public void setType(int currentType, String type, String catalogCodes, String city, String createTime, String tagCode) {
        this.currentType = currentType;
        this.type = type;
        this.city = city;
        this.createTime = createTime;
        this.tagCode = tagCode;
        this.catalogCodes = catalogCodes;
    }

    public void setGroupCode(String groupCode) {
        this.groupCode = groupCode;
    }
}
